var url = location.href.split("/");
var htp =document.location.protocol;
var baseUrl = htp+"//" + url[2] + "/" + url[3];
var dictList;
var $tableClick;
var parameterData;
(function ($) {
    var pageNo;
    var pageSize = 10;
    var lastpage;
        //数据字典
    $.UCPDict = {};
    //请求数据字典
    $.UCPPostDict = function (dictCate) {
        $.ajax({
            url: baseUrl + '/sys/dict/listData',
            data: "type=" + dictCate,
            async: false,
            success: function (data) {
                /*var resultOjbResult = eval('('+data.result+')');
		    	var dataItem=resultOjbResult['Dictinfo'];*/
                $.UCPDict["dict" + dictCate] = data;
            }
        });
    };
    $.fn.ECDATable = function () {
        var $this = $(this);
        var $ths = [];
        var id = $this.attr("id");
        var paramdata = {};
        if (!pageNo) {
            pageNo = 1;
        }
        paramdata["pageNo"] = pageNo;
        paramdata["pageSize"] = pageSize;
        var operate = $this.attr("operate") == "true";
        var operatetype = $this.attr("operatetype") == "true";
        var cachehtml = $this.children("tbody").eq(0).html();
        $this.empty();
        $this.next().remove();
        cachehtml = "<tbody style = 'display:none;'>" + cachehtml + "</tbody>";
        $this.append(cachehtml);
        var html = "<thead class=\"text-c\"><tr>";
        $this.find("th").each(function () {
            var $th = {};
            $th.name = $(this).attr("name");
            $th.style = $(this).attr("style");
            $th.type = $(this).attr("type");
            $th.title = $(this).attr("title");
            $th.dictname = $(this).attr("dictname");
            $th.dateHerf = $(this).attr("dataHref");
            $th.dataInfo = $(this).attr("dataInfo");
            $ths.push($th);
            html += "<th style = '" + $th.style + "'>" + $th.title + "</th>";
        });
        html += "</tr></thead>";
        $this.prepend(html);
        var t = $("#" + $this.attr("bindForm")).serializeArray();
        $.each(t, function (i) {
           var $value = this.value;
           paramdata[this.name] =$value;      
        });
        var dataAction = $this.attr("dataAction");
        if (!dataAction) {
            return;
        }
        var url = baseUrl + dataAction;
        jQuery.ajax({
            url: url,
            accepts: {
                mycustomtype: 'application/x-www-form-urlencoded'
            },
            //data: submitdata,
            async: true,
            method: 'post',
            /*   dataType: 'json',*/
            data: paramdata,
            timeout: 60000,
            cache: false,
            success: function (page) {
                var tds = "<tbody class=\"text-c\">";
                pageSize = page.pageSize;
                pageNo = page.pageNo;
                pageCount =Math.ceil(page.count/pageSize);
                lastpage = page.lastpage;
                var data = page.list;
            	$this.parent().find('.content-table-three').remove();
            	$this.parent().find('.table-pages').remove();
                if (data) {
                	var html="";

                /*	$('.visit-information').find('.content_order').remove();*/
                	var tables= "";
                	if(data.length>0){                		
                		var type = $this.attr('bindType');
                		if(!type){
    	                	for (var i = 0; i < data.length; i++) {	                	
    	                		var _table='<table class="content-table-three" cellspacing="0" cellpadding="0">'+
    									'<thead>'+
    										'<tr>'+
    											'<td colspan="1">违规编号：<span class="color-operation">'+data[i].id+'</span></td>'+
    											'<td>违规类别：<span>'+isNUll(data[i].voilationParameter.vioType)+'</span></td>'+
    											'<td colspan="2">违规情形：<span title="'+isNUll(data[i].voilationParameter.vioSituation)+'">'+isNUll(data[i].voilationParameter.vioSituation)+'</span></td>'+
    											'<td></td>'+
    										'</tr>'+
    									'</thead>'+
    									'<tbody>'+
    										'<tr>'+
    											'<td>'+
    												'<p>大区：<span>'+isNUll(data[i].area)+'</span></p>'+
    												'<p>营业部：<span>'+isNUll(data[i].salesOffice)+'</span></p>'+
    												'<p>团队：<span>'+isNUll(data[i].team)+'</span></p>'+
    											'</td>'+
    											'<td>'+
    												'<p>大区系数：<span>'+isNUll(data[i].ratio)+'</span></p>'+
    												'<p>大区违规系数：<span>'+isNUll(data[i].regionVioRatio)+'</span></p>'+
    												'<p>大区扣分：<span>'+isNUll(data[i].areaDeduct)+'</span></p>'+
    											'</td>'+
    											'<td>'+
    												'<p>营业部系数：<span>'+isNUll(data[i].salesOfficeRatio)+'</span></p>'+
    												'<p>营业部违规系数：<span>'+isNUll(data[i].salesOfficeVioRatio)+'</span></p>'+
    												'<p>营业部扣分：<span>'+isNUll(data[i].salesOffieceDeduct)+'</span></p>'+
    											'</td>'+
    											'<td>'+
    												'<p>团队扣分：<span>'+isNUll(data[i].teamRatio)+'</span></p>'+
    												'<p>备注：<span>'+isNUll(data[i].remark)+'</span></p>'+
    												'<p>检查时间：<span>'+date(isNUll(data[i].violationDate))+'</span></p>'+
    											'</td>'+
    											'<td>'+
    												'<a class="btn-operation" href="seeViolation.html?id='+data[i].id+'">查看</a>'+
    												'<a class="btn-operation" href="editViolation.html?id='+data[i].id+'">编辑</a>'+
    												'<i data-href="/violations/delete" data-id="'+data[i].id+'" class="btn-operation" onclick="deleteInfo(this)">删除</i> '+
    												'<i class="btn-operation" data-id="'+data[i].id+'" onclick="seeInformation(this)">相关人员详情</i>'+
    											'</td>'+
    										'</tr>'+
    									'</tbody>'+
    								'</table>';              			
    	                		tables+=_table;					
                    	  }	
                		}
                		else{
    	                	for (var i = 0; i < data.length; i++) {	                	
    	                		var _table='<table class="content-table-three foreachClass" cellspacing="0" cellpadding="0">'+
			    	    					 '<thead>'+
			    							'<tr>'+
			    								'<td colspan="1">违规类别：<span name="vioType">'+isNUll(data[i].vioTypeLabel)+'</span></td>'+
			    								'<td colspan="2">违规情形：<span title="'+isNUll(data[i].vioSituationLabel)+'" name="vioSituation">'+isNUll(data[i].vioSituationLabel)+'</span></td>'+
			    								'<td colspan="2">检查时间：<span name="violationDate">'+date(isNUll(data[i].violationDate))+'</span></td>'+
			    							'</tr>'+
			    						'</thead>'+
    									'<tbody>'+
    									'<tr>'+
    										'<td>'+
    											'<p>违规人员：<span name="persionName">'+isNUll(data[i].persionName)+'</span></p>'+
    											'<p>工号：<span name="persionCode">'+isNUll(data[i].persionCode)+'</span></p>'+
    											'<p>个人扣分：<span name="persionDeduction">'+isNUll(data[i].persionDeduction)+'</span></p>'+
    										'</td>'+
    										'<td>'+
    											'<p>大区：<span name="area">'+isNUll(data[i].area)+'</span></p>'+
    											'<p>营业部：<span name="salesOffice">'+isNUll(data[i].salesOffice)+'</span></p>'+
    											'<p>团队：<span name="team">'+isNUll(data[i].team)+'</span></p>'+
    										'</td>'+
    										'<td>'+
    											'<p>大区系数：<span name="ratio">'+isNUll(data[i].ratio)+'</span></p>'+
    											'<p>大区违规系数：<span name="regionVioRatio">'+isNUll(data[i].regionVioRatio)+'</span></p>'+
    											'<p>大区扣分：<span name="areaDeduct">'+isNUll(data[i].areaDeduct)+'</span></p>'+
    										'</td>'+
    										'<td>'+
    											'<p>营业部系数：<span name="salesOfficeRatio">'+isNUll(data[i].salesOfficeRatio)+'</span></p>'+
    											'<p>营业部违规系数：<span name="salesOfficeVioRatio">'+isNUll(data[i].salesOfficeVioRatio)+'</span></p>'+
    											'<p>营业部扣分：<span name="salesOffieceDeduct">'+isNUll(data[i].salesOffieceDeduct)+'</span></p>'+
    										'</td>'+
    										'<td>'+
    											'<p>团队扣分：<span name="teamRatio">'+isNUll(data[i].teamRatio)+'</span></p>'+
    											'<p>备注：<span name="remark">'+isNUll(data[i].remark)+'</span></p>'+
    										'</td>'+
    									'</tr>'+
    								'</tbody>'+
    								'</table>';              			
    	                		tables+=_table;					
                    	  }
                		}

                	}
                }
                html =tables+'</ul>'; 
            	if(data){
            			  $this.after(html);
            		
            	}
              
                //body end
                //Page begin
        		if($this.attr("pagenation") != "false"){
					$this.parent().append(page.html);
				}
                $this.parent().find('.table-pages a').bind("click",function(){
			    	var dataPage = $(this).attr("data-page");
			    	var text = $(this).text();
			    	if(!isNaN(dataPage)){
			    		pageNo = $(this).attr("data-page");
			    	}else if(pageNo>1&&dataPage == "pre"){
			    		pageNo--
			    	}else if(pageNo<pageCount&&dataPage == "next"){
			    		pageNo++;
			    	}else if(text == "跳转"){
			    		dataPage = $(this).prev().val();
			    		if(1 <= dataPage <= pageCount){
			    			pageNo = dataPage;
			    		}else{
			    			return;
			    		}
			    	}else{
			    		return;
			    	}
			    	$("#" + $this.attr("bindClick")).click();
			    });
			    $this.find("th input[type='checkbox']").click(function(){
			    	$this.find("td input[type='checkbox']").prop("checked",$(this).is(":checked"));
			    });

            }
        });
    };
})(jQuery);


function getDictValue(dictname, type) {
    var label;
    for (var int = 0; int < dictList.length; int++) {
        if (dictList[int].type == dictname && dictList[int].value == type) {
            label = dictList[int].label;
        }
    }
    return label;
}
function setDate(val){
	var id = $(val).attr('id');
	var date = {
            elem: "#"+id, //选择ID为START的input  
            format: 'YYYY-MM-DD hh:mm', //自动生成的时间格式  
         
            //istime: true, //必须填入时间  
            istoday: false, //是否是当天
            festival: true, //是否显示节日
            start: laydate.now(1, "YYYY-MM-DD"), //设置开始时间为当前时间  
            choose: function (startdatas) {}
      };
      laydate(date);	
}
function date(time) {
    var now = new Date(time);
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var len_mon = month.toString().length;
    if (len_mon < 2) {
        month = '0' + (now.getMonth() + 1);
    };

    var date = now.getDate();
    var len_d = date.toString().length;
    if (len_d < 2) {
        date = '0' + (now.getDate());
    };

    var hours = now.getHours();
    var len_h = hours.toString().length;
    if (len_h < 2) {
        hours = '0' + now.getHours();
    };

    var minutes = now.getMinutes();
    var len_m = minutes.toString().length;
    if (len_m < 2) {
        minutes = '0' + now.getMinutes();
    };

    var seconds = now.getSeconds();
    var len_s = seconds.toString().length;
    if (len_s < 2) {
        seconds = '0' + now.getSeconds();
    };

    time = year + '-' + month + '-' + date +' ' +hours+':'+minutes;
    return time;
}
function getCookies()
{
	var c_name="userToken";
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=")
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    } 
  }
return ""
}
function getCookie(){
	var user = getCookies();
	if(user !=''){
		var users = user.split('-');
	
		return users[0];
	}
	return   '';
}
function getUserName(){
	var user = getCookies();
	if(user !=''){
		var users = {};
		var users = user.split('-');
		users.usercode = users[0];
		users.username =decodeURI(escape(users[1]));
		return users;
	}
	return  null;
}
function dataactivityPage(obj) {
    var href = $(obj).attr('data-href');
    var id = $(obj).attr('data-id');
    var usercode =$(obj).attr('usercode');
    var status = $(obj).attr('status');
    var user = getCookie();
    if(user =='' || usercode !=user){
    	 layer.msg("无法编辑非自己负责的活动信息！",{
	 		    offset:'30%'
	 		});
			 return;
    }
    if(status == 0){
    	  window.location.href = encodeURI(href + '?id=' + id);
    }
    if(status ==1){
    	  window.location.href = encodeURI('../activity/activityInformation.html' + '?id=' + id);
    }
  
}
function deleteInfo(obj) {
    var href = $(obj).attr('data-href');
    var id = $(obj).attr('data-id');
    var data = {};
    data.id = id;
    parent.setData(data);
    parent.layer.open({
	  type: 2,
      title: "删除提示",
      shadeClose: true,
      shade: 0.2,
      maxmin: true, //开启最大化最小化按钮
      area: ['400px', '280px'],
      content: baseUrl+'/static/html_EC/delete.html',
      end:function(){
          $("#violationsinfo").ECDATable();      
      }
    });
}
function isNUll(value){
	if(value){
		return value;
	}
	return '';
}
function getDictName(type,_value){
    var name = '';
    var dict = $.UCPDict["dict" + type];
    if (!dict) {
        $.UCPPostDict(type);
        dict = $.UCPDict["dict" + type]; //请求后重新获取
    }
    if (dict) {
        if (_value) {
        	name = _value.replace(/(^s*)|(s*$)/g, "").trim();
        } else {
        	name = "";
        }
        for (var k in dict) {
            if (dict[k] && dict[k].value && dict[k].value.trim() == _value) {
            	name = dict[k].label;
            }
        }
    } else {
        name = (dict[_value] ? dict[_value] : _value);
    }
    return name;
}
function getDictNames(type,_value){
    var name = '';
    var newval= '';
    var dict = $.UCPDict["dict" + type];
    if (!dict) {
        $.UCPPostDict(type);
        dict = $.UCPDict["dict" + type]; //请求后重新获取
    }
    if (dict) {
        if (_value) {
        	name = _value.replace(/(^s*)|(s*$)/g, "").trim();
        } else {
        	name = "";
        }
        var names = _value.split(',');
        for (var i = 0; i < names.length; i++) {
        	var n;
        	 for (var k in dict) {
                 if (dict[k] && dict[k].value && dict[k].value.trim() == names[i]) {
                 	n = dict[k].label;
                 }
             }
        	 newval = newval+"、"+n;
		}
       
    } else {
    	newval = (dict[_value] ? dict[_value] : _value);
    }
    return newval.substring(1, newval.length);
}
function link(obj) {
    var href = $(obj).attr('data-href');
    var info = $(obj).attr('data-info');
    var data = $.fn.getupdateData(obj);
    try {
        if (callDataType && typeof (callDataType) == "function") {
            callDataType(data);
        }
    } catch (e) {}
    var index = layer.open({
        type: 2,
        title: info,
        shadeClose: true,
        shade: 0.2,
        maxmin: true, //开启最大化最小化按钮
        area: ['870px', '400px'],
        content: href
    })
    parent.setScrollVal(index);
    parent.setLayer(layer);
	parent.setScrollTop();
}

function dataPage(obj) {
    var href = $(obj).attr('data-href');
    var id = $(obj).attr('code');
    var name =$(obj).attr('name');
    window.location.href = encodeURI(href + '?id=' + id+'='+name);
}

function contains(arr, obj) {
    var i = arr.length;
    while (i--) {
        if (arr[i] === obj) {
            return true;
        }
    }
    return false;
}
function selecttype(obj){
			var _parent_ul=$(obj).parent('.main-select1').find('.chose-select-val1');
			var _child_li=_parent_ul.children('li');
			var _save_input=$(obj).parent('.main-select1').find('.save-input-val1');
			var _input=$(obj).parent('.main-select1').find('input[data="data"]');
			var $this =$(obj);
			$(obj).hasClass('btn-hover-select')? $(obj).removeClass('btn-hover-select'): $(obj).addClass('btn-hover-select');
			_parent_ul.toggle();
			_child_li.on('click',function(){
				$(this).addClass('select-checked').siblings(_child_li).removeClass('select-checked');
				var liText=$(this).text();
				var liVal=$(this).attr('type');
				_save_input.attr('placeholder',liVal);
				_save_input.attr('value',liText);
				_input.attr('value',liVal);
				$('.chose-select-val1').hide();
				$this.removeClass('btn-hover-select');
				     if(_input.attr('value')==""){
						 $this.parent('.required-select').css('border','1px solid #eb6755');
						 $this.parent('.required-select').next().css('display','inline-block');
						 _input.focus();
					
						/* return false; */
					}
					else{	
						$this.parent('.required-select').css('border','1px solid #e6e6e6');
						$this.parent('.required-select').next().hide();
					}
				
			});
}
$(document).ready(function () {
    var $table = $('table[datatable="true"]');
    $tableClick = $("#" + $table.attr("bindClick"));
    var table = $table.ECDATable();
    $tableClick.click(function () {
        $('table[datatable="true"][bindClick="' + $(this).attr("id") + '"]').ECDATable();
    });

   /* $('#close').click(function () {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    })
    $('#closePage').click(function () {
        var href = $(this).attr('closehref');
        window.location.href = baseUrl + '/static/html_EC' + href;
    })
    $('.cleanBtn').click(function(){
    	var form = $(this).attr('data-form');
    	$('#'+form).find('input').each(function(){
    		if(!$(this).attr('ucptype')){
        		$(this).val('');
    		}
    		else{
    			var $this = $(this);
    			$this.attr('value','请选择'); 
    			$this.attr('placeholder',$(this).attr('value')); 
    			 
    		}
    	})
    	$('#'+form).find('select').each(function(){
    		console.log(this);
    	$(this).parent().find('button').attr('title','');
    	$(this).parent().find('button').find('span').text('可以多选');
    	$(this).parent().find('div').find('ul').find('li').each(function(){
    		$(this).removeClass('selected');
    	 })
    	 $(this).parent().find('button').find('span').eq(1).hide();
    	})
    })
    $('button[data-type="delete"]').click(function () {
        var form = $(this).attr('data-form');
        var info = $(this).attr('data-info');
        var url = $(this).attr('data-url');
        var tableId = $(this).attr('data-id');
        var tableClick = $(this).attr('data-click');
        var data = $('#' + tableId).getCheckData();
        if (data.length == 0) {
            layer.msg("请选择一条数据", {
                offset: '30%'
            });
            return;
        }
        
       var  index = layer.open({
    	    type:1,
            content: '<div class="lineHeught">您确认要'+info+'吗？</div>',
            btn: ['确认', '关闭'],
            area: ['300px', '200px'],
            shadeClose: false,
            offset: '30%',
            yes: function (index) {
                    layer.close(index);
                    var datas = {};
                    var ids = "";
                    for (var i = 0; i < data.length; i++) {
                        ids += "," + data[i].id;
                    }
                    datas.ids = ids.substring(1, ids.length);
                    jquerypost(url, datas, true, $('#' + tableClick));
                },
                no: function () {}
        });
       parent.setScrollVal(index);
       parent.setLayer(layer);
	   parent.setScrollTop();
    })*/

})